<template>
  <div class="login-wrapper">
    <div class="login-box">
      <el-row style="justify-content: center;">
        <div class="input-box" >
          <input type="text" required="true" v-model="username" />
          <span>username</span>
        </div>
      </el-row>
      
      <el-row style="justify-content: center;">
        <div class="input-box">
          <input type="password" required="true" v-model="password"/>
          <span>password</span>
        </div>
      </el-row>

      <el-row style="justify-content: center;">
        <div class="buttons">
          <el-button @click="goToMain" size="large" type="primary" style="width: 80px;">LOGIN</el-button>
          <!-- <el-button @click="goToLogout" size="large" style="width: 80px;">退出</el-button> -->
          <el-button @click="registerVisible = true" size="large" style="width: 80px;">REGISTER</el-button>
        </div>
      </el-row>
      <el-dialog v-model="registerVisible"
        title="注册" width="30%">
        <el-form label-width="150px">
          <el-form-item label="USERNAME" >
            <el-input v-model="username" />
          </el-form-item> 
          <el-form-item label="PASSWORD">
            <el-input v-model="password" />
          </el-form-item> 
          <el-form-item label="Nickname">
            <el-input v-model="nickname" />
          </el-form-item> 
          <el-form-item label="roleId">
            <el-select v-model="roleId" placeholder="Select">
              <el-option label="用户" value="1"></el-option>
              <el-option label="创作者" value="2"></el-option>
              <el-option label="管理员" value="3"></el-option>
            </el-select>     
          </el-form-item>
          <el-form-item label="gender">
            <el-select v-model="gender" placeholder="Select">
              <el-option label="Male" value="true"></el-option>
              <el-option label="Female" value="false"></el-option>
            </el-select>     
          </el-form-item>
          <el-form-item label="Email">
            <el-input v-model="email" />
          </el-form-item>
          <el-form-item label="Birth">
            <el-date-picker
              v-model="birth"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="Birthdate"
              style="width: 100%"
            />
          </el-form-item>
          <el-form-item label="Description">
            <el-input type="textarea" v-model="description"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="registerUser" size="large" style="width: 60px;">提交</el-button>
            <el-button @click="registerVisible = false" size="large" style="width: 60px;">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts">
import axios from 'axios'
import { ElMessage } from 'element-plus'

export default {
  name: 'login',
  data() {
    return {
      username: '',
      password: '',
      nickname: '',
      gender: '',
      email: '',
      description: '',
      registerVisible: false,
      roleId: '',
      birth: '',
    }
  },
  methods: {
    registerUser() {
      let params = {
        username: this.username,
        password: this.password,
        nickname: this.nickname,
        email: this.email,
        gender: this.gender=='true' ? true : false,
        roleid: this.roleId,
        birth: this.birth,
      }
      console.log(params)
      axios.request({
        method: 'post',
        url: '/api/register',
        params: params,
      }).then((res) => {
        console.log(res.data)
        if (!!res.data.success) {
          this.registerVisible = false
          ElMessage({
            message: 'Successfully resgistered!',
            type: 'success',
          })
        } else {
          ElMessage({
            message: 'Registeration Failed : ' + res.data.message,
            type: 'error',
          })
        }
      })
    },
    goToMain() {
      axios.request({
        method: 'post', 
        url: '/api/login',
        params: {
          username: this.username,
          password: this.password
        },
      }).then((res) => {
        // console.log(res.data)
        if (!!res.data.success) {
          this.$emit('login')
          this.$emit('setAva', res.data.nickname, res.data.userid, res.data.roleid)
          ElMessage({
            message: 'Successfully Login!',
            type: 'success'
          })
          this.$router.push('/home')
        } else {
          ElMessage({
            message: 'Login Failed : ' + res.data.message,
            type: 'error',
          })
          console.log(res.data.message)
        }
      })
      // console.log(this.username, ' ', this.password)
      
    },
  }
}
</script>

<style scoped>

* {
  /* margin: 0; */
  padding: 0;
  box-sizing: border-box;
}

.login-wrapper {
  margin: 0;
  padding: 0;
  height: 100%;
  display: flex;
  align-items: center;
}

.login-box {
  margin: 0 auto;
  /* padding: 20px 20px 0px 20px; */
  height: 300px;
  width: 600px;
  /* border: 1px solid; */
  box-shadow: var(--el-box-shadow);
  border-radius: 20px;
  /* justify-content: center; */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 30px;
}

.input-box {
  position: relative;
  width: 250px;
}

.input-box input {
  width: 100%;
  padding: 10px;
  border: 1px solid rgba(0, 0, 0, 0.25);
  /* background: #1d2b3a; */
  border-radius: 5px;
  outline: none;
  color: #1d2b3a;
  font-size: 1em;
}

.input-box span {
  position: absolute;
  left: 0;
  padding: 10px;
  pointer-events: none;
  font-size: 1em;
  color: rgba(0, 0, 0, 0.25);
  text-transform: uppercase;
  transition: 0.5s;
}


.input-box input:valid ~ span,
.input-box input:focus ~ span {
  color: #00dfc4;
  transform: translateX(10px) translateY(-7px);
  
  font-size: 0.65em;
  padding: 0 10px;
  background: white;
  border-left: 1px solid #00dfc4;
  border-right: 1px solid #00dfc4;
  letter-spacing: 0.2em;
  /* transition: 0.5s; */
}

.input-box:nth-child(2) input:valid ~ span,
.input-box:nth-child(2) input:focus ~ span {
  background: #00dfc4;
  color: #1d2b3a;
  border-radius: 2px;
}

.input-box input:valid,
.input-box input:focus {
  border: 1px solid #00dfc4;
  transition: 0.5s;
}
</style>